package com.ruoyi.web.controller.project.controller;

import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.controller.project.domain.TOrderRecord;
import com.ruoyi.web.controller.project.service.ITOrderRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 订单记录Controller
 * 
 * @author ruoyi
 * @date 2025-06-27
 */
@Slf4j
@RestController
@RequestMapping("/project/record")
public class TOrderRecordController extends BaseController
{
    @Autowired
    private ITOrderRecordService tOrderRecordService;


    /**
     * 获取订单总金额
     */
    @GetMapping("/getTotal")
    public AjaxResult getTotalAmount(TOrderRecord tOrderRecord)
    {
        if (!SecurityUtils.getUserId().equals(1L) && !SecurityUtils.getUserId().equals(102L)){
            tOrderRecord.setUserId(SecurityUtils.getUserId());
        }
        HashMap<String, Object> result = tOrderRecordService.getTotal(tOrderRecord);
        return success(result);
    }


    @Log(title = "订单记录", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport,Long userId) throws Exception
    {
        log.debug("userId={}",userId);
        ExcelUtil<TOrderRecord> util = new ExcelUtil<TOrderRecord>(TOrderRecord.class);
        List<TOrderRecord> tOrderRecordList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = "";
        try {
            message = tOrderRecordService.importRecord(tOrderRecordList, updateSupport, operName,userId);
        } catch (Exception e) {
            log.debug(e.getMessage());
        }

        return success(message);
    }

    /**
     * 查询订单记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TOrderRecord tOrderRecord)
    {
        startPage();
        if (!SecurityUtils.getUserId().equals(1L) && !SecurityUtils.getUserId().equals(102L)){
            tOrderRecord.setUserId(SecurityUtils.getUserId());
        }
        List<TOrderRecord> list = tOrderRecordService.selectTOrderRecordList(tOrderRecord);
        return getDataTable(list);
    }

    /**
     * 导出订单记录列表
     */
    @Log(title = "订单记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TOrderRecord tOrderRecord)
    {
        if (!SecurityUtils.getUserId().equals(1L) && !SecurityUtils.getUserId().equals(102L)){
            tOrderRecord.setUserId(SecurityUtils.getUserId());
        }
        List<TOrderRecord> list = tOrderRecordService.selectTOrderRecordList(tOrderRecord);
        ExcelUtil<TOrderRecord> util = new ExcelUtil<TOrderRecord>(TOrderRecord.class);
        util.exportExcel(response, list, "订单记录数据");
    }

    /**
     * 获取订单记录详细信息
     */
    @GetMapping(value = "/{recordId}")
    public AjaxResult getInfo(@PathVariable("recordId") String recordId)
    {
        return success(tOrderRecordService.selectTOrderRecordByRecordId(recordId));
    }

    /**
     * 新增订单记录
     */
    @Log(title = "订单记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TOrderRecord tOrderRecord)
    {
        return toAjax(tOrderRecordService.insertTOrderRecord(tOrderRecord));
    }

    /**
     * 修改订单记录
     */
    @Log(title = "订单记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TOrderRecord tOrderRecord)
    {
        return toAjax(tOrderRecordService.updateTOrderRecord(tOrderRecord));
    }

    /**
     * 删除订单记录
     */
    @Log(title = "订单记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{recordIds}")
    public AjaxResult remove(@PathVariable String[] recordIds)
    {
        return toAjax(tOrderRecordService.deleteTOrderRecordByRecordIds(recordIds));
    }
}
